package com.gcit.seniorWish.web.modular.examinee.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gcit.seniorWish.core.node.SelectNode;
import com.gcit.seniorWish.web.common.constant.Const;
import com.gcit.seniorWish.web.common.constant.cache.Cache;
import com.gcit.seniorWish.web.common.constant.cache.CacheKey;
import com.gcit.seniorWish.web.common.constant.enums.ManagerStatusEnum;
import com.gcit.seniorWish.web.common.constant.enums.RoleEnum;
import com.gcit.seniorWish.web.common.constant.enums.UserTypeEnum;
import com.gcit.seniorWish.web.common.constant.factory.ConstantFactory;
import com.gcit.seniorWish.web.common.persistence.dao.SeniorSchoolClassMapper;
import com.gcit.seniorWish.web.common.persistence.dao.SeniorSchoolMapper;
import com.gcit.seniorWish.web.common.persistence.model.Role;
import com.gcit.seniorWish.web.common.persistence.model.SeniorSchool;
import com.gcit.seniorWish.web.common.persistence.model.SeniorSchoolClass;
import com.gcit.seniorWish.web.common.persistence.model.User;
import com.gcit.seniorWish.web.core.shiro.ShiroKit;
import com.gcit.seniorWish.web.modular.examinee.dao.SeniorSchoolClassDao;
import com.gcit.seniorWish.web.modular.examinee.service.ISeniorSchoolClassService;
import com.gcit.seniorWish.web.modular.system.service.IRoleService;
import com.gcit.seniorWish.web.modular.system.service.IUserMgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 班级信息表 服务实现类
 * </p>
 *
 * @author tonglu
 * @since 2018-03-19
 */
@Service
public class SeniorSchoolClassServiceImpl extends ServiceImpl<SeniorSchoolClassMapper, SeniorSchoolClass>
		implements ISeniorSchoolClassService {

	@Resource
	private SeniorSchoolClassDao seniorSchoolClassDao;

	@Resource
	private SeniorSchoolClassMapper seniorSchoolClassMapper;

	@Resource
	private SeniorSchoolMapper seniorSchoolMapper;

	@Autowired
	private IUserMgrService userMgrService;

	@Autowired
	private IRoleService roleService;

	/**
	 * 查询学校班级列表
	 *
	 * @param schoolId 学校id
	 * @author tonglu
	 * @date 2018-03-19 下午 3:22
	 */
	@Override
	public List<SelectNode> listSchoolClassForSelect2(String term, String schoolId, String classId) {
		List<SelectNode> selectList = new ArrayList<>();
		List<SeniorSchoolClass> classes = null;
		if (StrUtil.isNotBlank(classId)) {
			if (StrUtil.isNotBlank(term)) {
				term = "%" + term + "%";
			}
			classes = seniorSchoolClassDao.listSchoolClass(term, schoolId, classId);
			SelectNode node = new SelectNode();
			node.setId(classes.get(0).getId());
			node.setText(classes.get(0).getSeniorClassCode() + ":" + classes.get(0).getSenoirClassName());
			selectList.add(node);
		} else {
			EntityWrapper<SeniorSchoolClass> ew = new EntityWrapper<SeniorSchoolClass>();
			ew.eq("senoir_school_id", schoolId);
			if (StrUtil.isNotBlank(term)) {
				ew.andNew().like("senoir_class_name", term).or().like("senoir_class_code", term);
			}
			classes = seniorSchoolClassMapper.selectList(ew);
			for (SeniorSchoolClass c : classes) {
				SelectNode node = new SelectNode();
				node.setId(c.getId());
				node.setText(c.getSeniorClassCode() + ":" + c.getSenoirClassName());
				selectList.add(node);
			}
		}
		return selectList;
	}

	/**
	 * 合并初中学校班级信息
	 *
	 * @param schoolClass 班级信息
	 * @author gaozhichang
	 * @date 2018-03-29 上午 10:25
	 */
	@Override
	@Transactional
	@CacheEvict(value = Cache.BUSS, key = "'" + CacheKey.SENIOR_CLASS + "'+#schoolClass.id")
	public void mergeSeniorSchoolClass(SeniorSchoolClass schoolClass) {
		//id不存在，新增班级信息
		if (StrUtil.isBlank(schoolClass.getId())) {
			this.seniorSchoolClassMapper.insert(schoolClass);
			SeniorSchool school = ConstantFactory.me().getSeniorSchoolById(schoolClass.getSenoirSchoolId());
			// 插入用户表信息
			User user = new User();
			user.setType(UserTypeEnum.TEACHING.getValue());
			user.setAccount("szwz" + school.getSchoolCode() + schoolClass.getSeniorClassCode());
			user.setName(schoolClass.getSenoirClassName());
			// 完善账号信息
			user.setSalt(ShiroKit.getRandomSalt(5));
			user.setPassword(ShiroKit.md5(Const.SCHOOLORCLASS_PWD, user.getSalt()));
			user.setDeptId(Const.DEFAULT_DEPT_ID);
			user.setStatus(ManagerStatusEnum.ABLE.getCode());
			user.setSchoolId(school.getId());
			user.setClassId(schoolClass.getId());
			userMgrService.insert(user);
			Role role = roleService.selectOne(Condition.create().eq("name", RoleEnum.CLASS_TEACHER.getDes()));
			userMgrService.setRoles(user.getId(), role.getId());
			//id存在，修改班级信息，并且更改账号名称
		} else {
			this.seniorSchoolClassMapper.updateById(schoolClass);
			//根据登录名查询对应的用户，更改班级名称
			SeniorSchool school = ConstantFactory.me().getSeniorSchoolById(schoolClass.getSenoirSchoolId());
			User user = userMgrService.selectOne(Condition.create().setSqlSelect("id,name").eq("account",
					"szwz" + school.getSchoolCode() + schoolClass.getSeniorClassCode()));
			//当用户存在并且名称不同了。修改用户信息
			if (user != null) {
				user.setName(schoolClass.getSenoirClassName());
				user.setSchoolId(schoolClass.getSenoirSchoolId());
				userMgrService.updateById(user);
			}
		}
	}
}
